/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import {store} from "@mall/api-services";
import classNames from "classnames";
import * as React from "react";
import Flex from "../components/flex";
import Icon from "../icon";
import ProductList from "../product/product-list";
import "./search-result.scss";

enum SearchSort {
    BestDesc,
    SalesDesc,
    DateDesc,
    PriceAsc,
    PriceDesc,
}


interface SearchResultProps {
    name?: string;
}

interface SearchResultState {
    products: store.Product[];
    searchSort: SearchSort;
}

export default class SearchResult extends React.Component<SearchResultProps, SearchResultState> {


    public constructor(props: Readonly<SearchResultProps>) {
        super(props);

        this.state = {
            products: [],
            searchSort: SearchSort.BestDesc,
        };
    }

    public componentDidMount(): void {
        this.refreshProducts();
    }

    public componentDidUpdate(prevProps: Readonly<SearchResultProps>,
                              prevState: Readonly<SearchResultState>, snapshot?: any): void {

        if (this.props.name !== prevProps.name) {
            this.refreshProducts();
        }
    }

    public render() {
        const {products, searchSort} = this.state;

        return (
            <div className="search-result">
                <header>
                    <Flex className="search-result-sort-bar"
                          alignItems="center"
                          justifyContent="space-between">
                        <span className={classNames("sort-button", {
                            "active": searchSort === SearchSort.BestDesc,
                        })} onClick={() => this.setState({searchSort: SearchSort.BestDesc})}>综合</span>
                        <span className={classNames("sort-button", {
                            "active": searchSort === SearchSort.SalesDesc,
                        })} onClick={() => this.setState({searchSort: SearchSort.SalesDesc})}>销量</span>
                        <span className={classNames("sort-button", {
                            "active": searchSort === SearchSort.DateDesc,
                        })} onClick={() => this.setState({searchSort: SearchSort.DateDesc})}>新品</span>
                        <span className={classNames("sort-button", {
                            "active": searchSort === SearchSort.PriceAsc,
                        })} onClick={() => this.setState({searchSort: SearchSort.PriceAsc})}>价格</span>
                        <Flex className="sort-button"
                              alignItems="center">筛选<Icon type="filter" size="xxs"/></Flex>
                    </Flex>
                </header>
                <div className="search-result-content">
                    <ProductList products={products}/>
                </div>
            </div>
        );
    }

    private refreshProducts() {
        const {name = ""} = this.props;
        store.ProductServiceV1.getProducts()
            .name(name)
            .list()
            .then(pageProducts => this.setState({products: pageProducts.elements}));
    }
}
